<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 16:55
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Listeners\LocationLinkSourceListener;
use SDXApi\Model\HomeTemplateStencilImageModel;

/**
 * Class HomeTemplateImageService
 * @package SDXApi\Service
 */
class HomeTemplateImageService extends TemplateAbstract
{
	/**
	 * @var HomeTemplateStencilImageModel
	 */
	protected $templateStencilImageModel;

	/**
	 * HomeTemplateImageService constructor.
	 */
	public function __construct()
	{
		$this->templateStencilImageModel = new HomeTemplateStencilImageModel();
	}

	/**
	 * 处理图片模块
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	public function handle($stencil = array(), $userId = 0, $wid = 8)
	{
        $source = $this->sourceApp();
        $cacheName = 'home_template_image_id_' . $stencil['id'] . '_app_' . $source;

//		获取缓存
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->stencilImage($stencil, $userId, $wid);
			$this->cacheData($cacheName, $data, 3600);
		}

        if (isset($data['link_data']) && empty($data['link_data'])) {
            $data['link_data'] = new \stdClass();
        }

		return $data;
	}

	/**
	 * 处理图片模块数据
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	protected function stencilImage($stencil = array(), $userId = 0, $wid = 8)
	{
		$images = $this->findTemplateStencilImageByStencilId($stencil['id']);

        $imageSize = array(
            1	=>	'750px 360px',
            3	=>	'750px * 260px',
            4	=>	'750px 180px',
            5	=>	'345px 400px',
            6	=>	'365px 175px',
            7	=>	'223px 320px',
            8   =>  '353px 230px',
            9   =>  '234px 354px',
            10  =>  '342px 420px',
            11  =>  '750px 220px',
            12  =>  '750px 300px',
            13  =>  '236px 236px（一行三图）',
            14  =>  '365px 260px（一行两图）',
            15  =>  '365px 440px（一行两图）',
            16  =>  '250px 340px',
        );

        $locationLinkListener = new LocationLinkSourceListener();

		foreach ($images as $key => $value) {
//			修改图片处理地址
			$images[$key]['url'] = self::checkImageUrlReplace($value['url']);
            $link_data = $locationLinkListener->onCallLink($value['link']);
            if (! empty($link_data)) {
                $images[$key]['link_data'] = $link_data;
            }

			if (empty($stencil['size'])) {
				$stencil['size'] = $value['size'];
				$stencil['size_message'] = $imageSize[$stencil['size']];
			}

			unset($images[$key]['size']);
		}

		if (count($images) > 1) {
			$images = self::checkDataSort($images);
		}

		$stencil['images_list'] = $images;

		return $stencil;
	}

	/**
	 * 获取模块所有图片
	 * Created by Jerry
	 * @param $stencilId
	 * @return mixed
	 */
	protected function findTemplateStencilImageByStencilId($stencilId)
	{
		return $this->templateStencilImageModel->find_template_stencil_images_by_stencil_id($stencilId);
	}

	/**
	 * 排序
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @return mixed
	 */
	protected static function checkDataSort($data, $sortKey = 'sort')
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value[$sortKey];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}

}